﻿using Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace YouKuFilm
{
    /// <summary>
    /// 优酷电影数据采集
    /// </summary>
    public class CollectYouKuMovie
    {
        public List<YouKuFilmItem> AllFilms;

        public void DoGetCurFilms()
        {
            AllFilms = new List<YouKuFilmItem>();

            MovicURL urlGetter = new MovicURL();

            string allUrlStr = urlGetter.GetCurMoiveURLString();

            GetVideoInfosFormURL(allUrlStr, true);
        }

        public void DoGetAllFilms()
        {
            AllFilms = new List<YouKuFilmItem>();

            MovicURL urlGetter = new MovicURL();

            List<string> allUrlStr = urlGetter.GetALLMoiveURLString();

            foreach (string str in allUrlStr)
            {
                GetVideoInfosFormURL(str, true);
                System.Threading.Thread.Sleep(300);
            }
        }

        /// <summary>
        /// 获取页面上有用的视频信息
        /// </summary>
        /// <param name="_url">列表页面地址，不含.html后缀</param>
        /// <param name="_isF">是否是第一页，第一页要获取总页数</param>
        /// <returns></returns>
        List<YouKuFilmItem> GetVideoInfosFormURL(string _url, bool _isF)
        {
            List<YouKuFilmItem> _allVideoInfos = new List<YouKuFilmItem>();
            try
            {
                MyWebClient myWebClient = new MyWebClient();
                Stream myWebStream = myWebClient.OpenRead(_url + ".html");
                StreamReader sr = new StreamReader(myWebStream, System.Text.Encoding.GetEncoding("utf-8"));
                String strHTML = sr.ReadToEnd();
                myWebStream.Close();
                if (!string.IsNullOrEmpty(strHTML))
                {
                    //通过正则表达式截取页面有用信息区块
                    //起始字符串
                    string startStr = "<div class=\"box-series\"><ul class=\"panel\">";
                    //结束字符串 
                    string endStr = "<ul class=\"yk-pages\">";
                    Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline);
                    string MidStr = rg.Match(strHTML).Value;
                    if (!string.IsNullOrEmpty(MidStr))
                    {
                        //使用正则表达式，截取所有的信息
                        //开始字符串 
                        string s11 = "<div class=\"p-thumb\">";
                        //结束字符串 
                        string s21 = "</div><ul";
                        Regex rg1 = new Regex("(?<=(" + s11 + "))[.\\s\\S]*?(?=(" + s21 + "))", RegexOptions.Multiline | RegexOptions.Singleline);

                        MatchCollection matches = rg1.Matches(MidStr);

                        System.Collections.IEnumerator enu = matches.GetEnumerator();
                        while (enu.MoveNext() && enu.Current != null)
                        {
                            Match match = (Match)(enu.Current);
                            YouKuFilmItem vd = GetvideoInfoformurlstr(match.Value);
                            if (vd != null)
                            {
                                if (AllFilms.Contains(vd) == false)
                                {
                                    AllFilms.Add(vd);
                                }
                                _allVideoInfos.Add(vd);
                            }
                        }
                    }

                    if (_isF)
                    {
                        int sum = GeiPageCount(strHTML);
                        for (int i = 2; i <= sum; i++)
                        {
                            GetVideoInfosFormURL(_url + "_p_" + i.ToString(), false);
                        }
                    }
                }
            }
            catch { }

            return _allVideoInfos;
        }

        /// <summary>
        /// 从页面代码中获取列表页的总页数
        /// </summary>
        /// <param name="htmlstr"></param>
        /// <returns></returns>
        int GeiPageCount(string htmlstr)
        {
            int sum = 0;
            try
            {
                int index1 = htmlstr.LastIndexOf("</a></li><li class=\"next\" title=\"下一页\">");

                if (index1 != -1)
                {
                    int index2 = htmlstr.LastIndexOf(">", index1);
                    if (index2 != -1)
                    {
                        string sindex = htmlstr.Substring(index2 + 1, index1 - index2 - 1);
                        sindex = sindex.Trim();
                        sum = int.Parse(sindex);
                    }
                }
            }
            catch { }

            return sum;
        }

        YouKuFilmItem GetvideoInfoformurlstr(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                //开始字符串 
                string s1 = "<a href=\"";
                //结束字符串 
                string s2 = "\" title=\"";
                Regex rg = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + s2 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
                string str1 = rg.Match(str).Value;

                if (!string.IsNullOrEmpty(str1))
                {
                    YouKuFilmItem yk = new YouKuFilmItem();
                    yk.VideoURL = str1;

                    //开始字符串 
                    string s11 = "title=\"";
                    //结束字符串 
                    string s21 = "\"";
                    Regex rg1 = new Regex("(?<=(" + s11 + "))[.\\s\\S]*?(?=(" + s21 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
                    string str11 = rg1.Match(str).Value;
                    yk.VideoName = str11;

                    //开始字符串 
                    string s111 = @"/v_show/id_";
                    //结束字符串 
                    string s211 = ".html";
                    Regex rg11 = new Regex("(?<=(" + s111 + "))[.\\s\\S]*?(?=(" + s211 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
                    yk.VideoId = rg11.Match(yk.VideoURL).Value;

                    //开始字符串 
                    string s1111 = "<img class=\"quic\" _src=\"";
                    //结束字符串 
                    string s2111 = "\"";
                    Regex rg111 = new Regex("(?<=(" + s1111 + "))[.\\s\\S]*?(?=(" + s2111 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
                    yk.VideoImgURL = rg111.Match(str).Value;

                    return yk;
                }

            }
            return null;
        }
    }
}
